package com.example.myapplication.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

import static android.R.layout.simple_expandable_list_item_1;

/**
 * A simple {@link Fragment} subclass.
 */
public class f3 extends Fragment {


    View view;
    Context context;
    private EditText editOne;
    private EditText editTwo;
    private ListView f3Listview;
    private CheckBox boxAdd;
    private CheckBox boxSubtract;
    private CheckBox boxMultiply;
    private CheckBox boxDivide;
    ArrayAdapter<String> arrayAdapter;
    public f3() {
        // Required empty public constructor
    }
    ArrayList<String> list=new ArrayList<String>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_f3, container, false);
        context = getContext();
        initView();
        intData();
        calculate();
        return view;
    }
    /*
    初始化适配器
     */
    public void intData(){
        arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_expandable_list_item_1,list);
        f3Listview.setAdapter(arrayAdapter);
    }

    /*
    进行加减乘除运算
     */
public void calculate(){
    yunSuan(boxAdd,boxAdd.getText().toString());
    yunSuan(boxSubtract,boxSubtract.getText().toString());
    yunSuan(boxMultiply,boxMultiply.getText().toString());
    yunSuan(boxDivide,boxDivide.getText().toString());

}
/*
运算方法
 */
double result;
public void yunSuan(final CheckBox box, final String s){

    box.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String one=editOne.getText().toString();
            final String two=editTwo.getText().toString();
            if (one.equals("")||two.equals("")) {
                Toast.makeText(context, "请输入两个数", Toast.LENGTH_SHORT).show();
            } else {
                if (s.equals("+")){ result = Double.valueOf(one) + Double.valueOf(two);}
                if (s.equals("-")){ result = Double.valueOf(one) - Double.valueOf(two);}
                if (s.equals("*")){ result = Double.valueOf(one) * Double.valueOf(two);}
                if (s.equals("/")){ result = Double.valueOf(one) / Double.valueOf(two);}
                list.add(result+"="+one+s+two);
                arrayAdapter.notifyDataSetChanged();
            }
            box.setChecked(false);
        }

        });

}

    /*
    初始化控件
     */

    private void initView() {
        editOne = (EditText)view. findViewById(R.id.edit_one);
        editTwo = (EditText) view.findViewById(R.id.edit_two);
        f3Listview = (ListView)view. findViewById(R.id.f3_listview);
        boxAdd = (CheckBox) view.findViewById(R.id.box_add);
        boxSubtract = (CheckBox) view.findViewById(R.id.box_subtract);
        boxMultiply = (CheckBox)view. findViewById(R.id.box_multiply);
        boxDivide = (CheckBox) view.findViewById(R.id.box_divide);
    }
}
